const dbConfig = require('../dbConfig')
const mysql = require('mysql')

let deleteInfo = (sql, option) => {
    return new Promise((resolve, reject) => {
        try {
            let connection = mysql.createConnection(dbConfig)
            connection.connect()
            connection.query(sql, option, (err) => {
                connection.end()
                if (err) {
                    console.log('[SELECT ERROR]：', err.message)
                    reject({ 'errCode': '400', 'errMsg': 'fail' })
                }
                let obj = { 'errCode': '0', 'errMsg': 'success' }
                resolve(obj)
            })

        } catch (e) {
            console.log(new Date() + 'deleteStdInfo出错,错误信息:' + e)
            reject({ 'errCode': '400', 'errMsg': 'fail' })
        }
    })
}

let sendRes = (req, res, data) => {
    res.send(data)
}

let DeleteInfo = {
    deleteStudent: async (req, res) => {
        let sql1 = 'UPDATE student SET isShow = 0 WHERE Sno = ?'
        let sql2 = 'UPDATE SC SET isShow = 0 WHERE Sno = ?'
        let option = req.body.Sno
        await deleteInfo(sql1, option)
        let obj = await deleteInfo(sql2, option)
        sendRes(req, res, obj)
    },
    deleteCourse: async (req, res) => {
        let sql = `UPDATE course SET isShow = 0 WHERE Cno = ?`
        let option = req.body.Cno
        let obj = await deleteInfo(sql, option)
        sendRes(req, res, obj)
    },
    deleteSC: async (req, res) => {
        let sql = `UPDATE SC SET isShow = 0 WHERE Cno = ? AND Sno = ?`
        let option = []
        option[0] = req.body.Cno
        option[1] = req.body.Sno
        let obj = await deleteInfo(sql, option)
        sendRes(req, res, obj)
    }
}

module.exports = DeleteInfo